<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--
    Document   : index
    Created on : 26/11/2012, 11:20:41 AM
    Author     : Enrico Jimenez
    version    : 1.0
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%
            HttpSession variableSesion = request.getSession(false);
            if (variableSesion.getAttribute("nombreUsuario") == null) {
                response.sendRedirect("../");
            } else if (!dp.Seguridad.meAbren(request.getRequestURI(), variableSesion.getAttribute("codigoUsuario"))) {
                response.sendRedirect("../error.jsp");
            }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Administraci&oacute;n de Diccionario</title>
        <link type="text/css" rel="stylesheet" href="../lib/css/estilos.css">

        <link type="text/css" rel="stylesheet" href="../lib/css/redmond/jquery-ui-1.8.24.custom.css">

        <script type="text/javascript" src="../lib/js/jquery-1.8.2.min.js"></script>
        <script type="text/javascript" src="../lib/js/jquery-ui-1.8.24.custom.min.js"></script>
        <script type="text/javascript">
            $(function(){
                $("#btnInsertar").button();
                $("#btnGuardar").button();
                $("#btnEliminar").button();
                $("#btnInsertar").fadeOut(0);
                $("#btnGuardar").fadeOut(0);
                $("#btnEliminar").fadeOut(0);
            });
            function desactivar(){
                $("#btnInsertar").fadeOut(500);
                $("#btnGuardar").fadeOut(500);
                $("#btnEliminar").fadeOut(500);
            }
            function activar(){
                if(document.getElementById("listPalabra").value == ""){
                    $("#btnInsertar").fadeOut(500);
                    $("#btnGuardar").fadeOut(500);
                    $("#btnEliminar").fadeOut(500);
                }else if(document.getElementById("listPalabra").value == "@"){
                    $("#btnInsertar").fadeIn(500);
                    $("#btnGuardar").fadeOut(5);
                    $("#btnEliminar").fadeOut(5);
                }else{
                    $("#btnInsertar").fadeOut(10);
                    $("#btnGuardar").fadeIn(500);
                    $("#btnEliminar").fadeIn(500);
                }
            }

            function enviar(flag){
                document.getElementById("flagAccion").value = flag;
                document.getElementById("frmAdminPalabra").submit();
            }
        </script>
    </head>
    <body onload="">
        <form class="formulario" id="frmAdminPalabra" name="frmAdminPalabra" method="POST" action="<%= request.getRequestURI()%>"><br>
            <jsp:useBean id="beanDiccionarioDP" scope="page" class="dp.DiccionarioDP" />
            <div class="container">
                <h3>Administraci&oacute;n de Diccionario</h3>
                <div class="labels">
                    <label>Palabra a modificar:</label><br>
                    <label>Nuevo Palabra:</label><br>
                </div>
                <div class="inputs">
                    <select onclick="desactivar()" onblur="activar()" id="listPalabra" name="listPalabra" >
                        <option value=""></option>
                        <option value="@">Agregar Nuevo</option>
                        <c:forEach var="pal" items="${beanDiccionarioDP.palabrass}">
                            <option value="${pal.key}">${pal.value}</option>
                        </c:forEach>
                    </select><br>
                    <input type="text" id="txtPalabra" name="txtPalabra" value="" /><br>
                </div>
                <div class="zonaBtn">
                    &nbsp;<br>
                    <input onclick="enviar(1)" type="button" value="Insertar"  id="btnInsertar" name="btnInsertar" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input onclick="enviar(2)" type="button" value="Guardar"  id="btnGuardar" name="btnGuardar" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input onclick="enviar(3)" type="button" value="Eliminar"  id="btnEliminar" name="btnEliminar" />
                </div>
            </div>
            <div id="errores"></div>
            <input type="hidden" value="" id="flagAccion" name="flagAccion">
        </form>
    </body>
</html>
<%
            if (request.getParameter("flagAccion") != null) {
                int op = Integer.parseInt(request.getParameter("flagAccion"));
                try {
                    beanDiccionarioDP.setPalabra(request.getParameter("txtPalabra"));
                    beanDiccionarioDP.setIdPalabra(Integer.parseInt(request.getParameter("listPalabra")));
                } catch (Exception e) {
                    beanDiccionarioDP.setIdPalabra(-1);
                }
                switch (op) {
                    case 1://Insertar
                        beanDiccionarioDP.insertarPalabra();
                        break;
                    case 2://Modificar
                        beanDiccionarioDP.modificarPalabra();
                        break;
                    case 3://Eliminar
                        beanDiccionarioDP.eliminarPalabra();
                        break;
                    default:
                        System.out.print("Opcion escogida invalida");
                        break;
                }
            }
%>